/* Author: Nahuel F. Lema, Maxi Yañez

 */

var creaTuCuentaController = {
    errores: {
        1:'La contrase\u00f1a debe tener al menos 6 caracteres',
        2:'Las contrase\u00f1as no coinciden',
        3:'El mail ingresado no es v\u00e1lido'
    },

    validate: {

        get: function(){
            var componentsPath = scriptsController.defs.baseUrl + scriptsController.defs.componentsPath + 'jvalidation/',
            self = this;

            $.getScript(componentsPath + 'jquery.validate.min.1.9.js', function(){
                self.bind();
            });
        },

        bind: function(){
            var form = $(js.get.item('#crear-cuenta-form-container')[0]);

            $(js.get.item('#submit-form-login')[0]).click(function(e){
                e.preventDefault();
                if (form.valid()) {
                    creaTuCuentaController.submit()
                }
            });

            form.validate({
                rules: {
                    'email-field' : {
                        required : true,
                        email	 : true
                    },
                    'pass-field': {
                        required : true
                    },
                    're-pass-field' : {
                        required : true,
                        equalTo: "#pass-field"
                    },
                    'observaciones-field' : {
                        minlength: 10
                    },
                    'terminos-field' : {
                        required : true
                    }
                },
                messages: {
                    'email-field' : {
                        required : "Este campo es obligatorio",
                        email	 : "Debe ser un email"
                    },
                    'pass-field': {
                        required : "Este campo es obligatorio"
                    },
                    're-pass-field' : {
                        required : "Este campo es obligatorio",
                        equalTo: "Los campos no coinciden"
                    },
                    'observaciones-field' : {
                        minlength: "Ingrese un minimo de 10 caracteres"
                    },
                    'terminos-field' : {
                        required : "Ten&eacute;s que aceptar los t&eacute;rminos y condiciones"
                    }
                },
                submitHandler: function() {
                    $('#myModal').modal({
                        backdrop: false
                    });
                }
            });
        },

        init: function(){
            this.get();
        }
    },
    
    submit: function () {
        var self = this;
        $.ajax({
            url:scriptsController.defs.baseUrl+'controllers/usuarioController/create',
            type:'post',
            data:$('#crear-cuenta-form-container').serialize(),
            beforeSend: function () {
                $('#login-container').html('')
                scriptsController.loader.append($('#login-container'), 'big ');
            },
            success: function (data) {
                if (data!=0) {
                    alert(self.errores[data])
                } else {
                    scriptsController.loader.remove($('#login-container'));
                    $('#login-container').append($('#respuestaContainer').html())
                }
            }
        })
    },

    init: function(){
        this.validate.init();
    }
}

creaTuCuentaController.init();